import os 
import sys
from typing import List
import PySide6
from PySide6.QtCore import QObject, QEvent, Qt,QPoint
from PySide6.QtGui import QMouseEvent,QWindow

class EdgeResizeEventFilter(QObject):
   def __init__(self, window:QWindow, parent=None):
       super().__init__(parent)
       self.window:QWindow = window
       self.resizing = False
       self.resizeEdges:List[str] = []  # Active edges: "left", "right", "top", "bottom"
       self.borderWidth = 6   # Pixel threshold for edge detection
       self.startGlobalPos:QPoint = None
       self.lastGlobalPos :QPoint= None
   def eventFilter(self, watched: QObject, event: QMouseEvent)->bool:
       #print(f'event: {event}')
       if event.type() == QEvent.Type.MouseButtonPress:
           pos = event.pos()  # Local mouse position.
           self.startGlobalPos = event.globalPos()
           self.lastGlobalPos = event.globalPos()
           self.resizeEdges = self._detectEdges(pos)
           if self.resizeEdges:
               self.resizing = True
               return True
       elif event.type() == QEvent.Type.MouseMove:
           #print(f'pos: {pos}')
           if self.resizing:
               # Calculate the incremental delta from the last recorded global position.
               delta = event.globalPos() - self.lastGlobalPos
               geom = self.window.geometry()
               # Adjust right edge
               if "right" in self.resizeEdges:
                   geom.setWidth(geom.width() + delta.x())
               # Adjust bottom edge
               if "bottom" in self.resizeEdges:
                   geom.setHeight(geom.height() + delta.y())
               # Adjust left edge
               if "left" in self.resizeEdges:
                   geom.setX(geom.x() + delta.x())
                  # geom.setWidth(geom.width())
               if "top" in self.resizeEdges:
                   geom.setY(geom.y() + delta.y())
                   #geom.setHeight(geom.height() - delta.y())
               self.window.setGeometry(geom)
               self.lastGlobalPos = event.globalPos()
               return True
           else:
               # Update the cursor based on proximity to window edges.
               edges = self._detectEdges(event.pos())
               if edges:
                   if ("left" in edges and "top" in edges) or ("right" in edges and "bottom" in edges):
                       self.window.setCursor(Qt.CursorShape.SizeFDiagCursor)
                   elif ("right" in edges and "top" in edges) or ("left" in edges and "bottom" in edges):
                       self.window.setCursor(Qt.CursorShape.SizeBDiagCursor)
                   elif "left" in edges or "right" in edges:
                       self.window.setCursor(Qt.CursorShape.SizeHorCursor)
                   elif "top" in edges or "bottom" in edges:
                       self.window.setCursor(Qt.CursorShape.SizeVerCursor)
               else:
                   self.window.unsetCursor()

       elif event.type() == QEvent.Type.MouseButtonRelease:
           if self.resizing:
               self.resizing = False
               self.resizeEdges = []
               return True

       return super().eventFilter(watched, event)

   def _detectEdges(self, pos:QPoint):
       edges = []
       # Check horizontal edges
       if pos.x() <= self.borderWidth:
           edges.append("left")
       elif pos.x() >= self.window.width() - self.borderWidth:
           edges.append("right")
       # Check vertical edges
       if pos.y() <= self.borderWidth:
           edges.append("top")
       elif pos.y() >= self.window.height() - self.borderWidth:
           edges.append("bottom")
       return edges